<script lang="ts" setup>
import type { SessionEntity } from "@/typings/api/chat";
import { NInfiniteScroll } from "naive-ui";
import { computed, ref, toRefs } from "vue";
import { useUserStore } from "@/stores/module/user";
import { getAllSessions } from "@/api/chat";

const { userId } = toRefs(useUserStore());

// 会话列表
const sessionList = ref<SessionEntity[]>([]);

// 当前的会话id
const currentSessionId = ref<string>("");

// 获取sessions
const loadSessions = async () => {
  try {
    const { code, data } = await getAllSessions(userId.value);
    if (code === 200) {
      sessionList.value = data;
    }
  } catch (error) {}
};

// 点击会话
const handleSession = (session: SessionEntity) => {
  currentSessionId.value = session.id;
};

defineExpose({
  loadSessions,
  handleSession,
  sessionList,
  currentSessionId,
});
</script>

<template>
  <NInfiniteScroll>
    <template v-for="item in sessionList" :key="item.id">
      <div class="cursor-pointer hover-bg-gray-2 p-y-1 rd-2 text-center" @click="handleSession(item)">
        {{ item.name }}
      </div>
    </template>
  </NInfiniteScroll>
</template>

<style lang="less" scoped></style>
